#include "gui_defs.hpp"
#include "gui_base.hpp"

using namespace std;
using namespace cv;

/////////////////////////////////////////////////////////////////////////////////////
Mat GuiBase::dispMem = Mat(576, 720, CV_8UC3, Scalar(0, 0, 0));

void GuiBase::gbPixel(int x, int y, unsigned int color) {
	int width = dispMem.size().width;
	int chl = dispMem.channels();
	unsigned char *frame = dispMem.ptr<unsigned char>(0);

	*(frame + width * y * chl + x * chl + 0) = (color >> 0);		//B
	*(frame + width * y * chl + x * chl + 1) = (color >> 8);		//G
	*(frame + width * y * chl + x * chl + 2) = (color >> 16); 	//R
}

void GuiBase::gbPixel(Point pt, unsigned int color) {
	gbPixel(pt.x, pt.y, color);
}

void GuiBase::gbFillRect(Point start, Point end, unsigned int color) {
	// BGR
	if (color == 0) {
		rectangle(dispMem, start, end, SCALAR24(color), CV_FILLED);
	} else {
		int r = (color >> 16) & 0xFF, g = (color >> 8) & 0xFF, b = (color >> 0) & 0xFF;
		r = r * 15 / 16;
		g = g * 15 / 16;
		b = b * 15 / 16;

		for (int j = start.y; j < end.y; j++) {
			unsigned char *dstData = dispMem.ptr<unsigned char>(j);

			for (int i = start.x; i < end.x; i++) {
				dstData[i * 3 + 0] = b + dstData[i * 3 + 0] * 1 / 16;
				dstData[i * 3 + 1] = g + dstData[i * 3 + 1] * 1 / 16;
				dstData[i * 3 + 2] = r + dstData[i * 3 + 2] * 1 / 16;
			}
		}
	}
}

void GuiBase::gbDrawRect(Point start, Point end, unsigned int color, int w) {
	// BGR
	rectangle(dispMem, start, end, SCALAR24(color), w);
}

void GuiBase::gbLine(Point start, Point end, unsigned int color, int w) {
	// BGR
	line(dispMem, start, end, SCALAR24(color), w);
}

